#include <stdio.h>
#include <string.h>
#include "tokens.h"

YYSTYPE yylval;
int yylex(void);
int tokPos;
extern FILE *yyin;

char* toknames[] = {
"ID",     "STRING", "INT",    "COMMA",  "COLON",  "SEMICOLON", "LPAREN",
"RPAREN", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "DOT",       "PLUS",
"MINUS",  "TIMES",  "DIVIDE", "EQ",     "NEQ",    "LT",        "LE",    "GT",    "GE",
"AND",    "OR",     "ASSIGN", "ARRAY",  "IF",     "THEN",      "ELSE",  "WHILE", "FOR",
"TO",     "DO",     "LET",    "IN",     "END",    "OF",        "BREAK", "NIL",   "FUNCTION",
"VAR",    "TYPE"
};


char* tokname(tok) {
  return tok<257 || tok>299 ? "BAD_TOKEN" : toknames[tok-257];
}


int main(int argc, char*argv[])
{
    char* fname;
    int tok;
    if(argc != 2){
        printf("usage: ./bin filename\n");
        exit(1);
    }
    fname = argv[1];
    yyin = fopen(fname,"r");
    if(yyin == NULL){
        printf("input file open error\n");
        exit(0);
    }
    while(1){
        tok = yylex();
        if(tok == 0)break;
        switch(tok)
        {
            case ID:
            case STRING:{
                printf("%10s %4d %s\n",tokname(tok),tokPos,yylval.sval);
            }break;
            case INT:{
                printf("%10s %4d %d\n",tokname(tok),tokPos,yylval.ival);
            };break;
            default:{
                printf("%10s %4d\n",tokname(tok),tokPos);
            };break;
        }
    }

    return 0;
}